package com.yanze.schoolinformation.viewModule.movie;

import android.arch.lifecycle.ViewModel;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.HotMovieBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/4/26.
 */

public class HotMovieViewModel extends ViewModel {
    private OnMovieLoadListener listener;
    private Disposable mDisposable;

    public void setListener(OnMovieLoadListener listener) {
        this.listener = listener;
    }

    public void getHotMovie() {
        DataReqo.getInstant().getHotMovie(C.url.API_DOUBAN)
                .subscribe(new DeforObserver<HotMovieBean>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
            public void _onNext(HotMovieBean entity) {
                if (listener != null) {
                    listener.onSuccess(entity);
                }
            }

            @Override
            public void _onError(String msg) {
                if (listener != null) {
                    listener.onFailure(msg);
                }
            }
        });
    }

    public void onDestroy() {
        listener = null;
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
